# 测试对话历史记录功能
import sys
sys.stdout.reconfigure(encoding='utf-8')

from main import KnowledgeExplanationAgent

def test_chat_history():
    print("=== 测试对话历史记录功能 ===")
    
    # 创建智能体实例
    agent = KnowledgeExplanationAgent()
    user_id = "test_user_001"
    
    # 测试问题列表
    test_questions = [
        "你好",
        "什么是所有权？",
        "如何使用Vec？",
        "Rust和C++有什么区别？"
    ]
    
    print("\n1. 发送多个问题并记录历史:")
    for i, question in enumerate(test_questions, 1):
        print(f"\n问题 {i}: {question}")
        result = agent.process_question(user_id, question)
        
        if result['status'] == 'success':
            print(f"分类: {result['classification']['label']}")
            print(f"回答长度: {len(result['answer_markdown'])}")
        else:
            print(f"错误: {result['message']}")
    
    print("\n2. 检查对话历史:")
    context = agent.context_manager.get_context(user_id)
    print(f"历史记录数量: {len(context)}")
    
    for i, turn in enumerate(context, 1):
        print(f"\n轮次 {i}:")
        print(f"  问题: {turn['question']}")
        print(f"  分类: {turn['classification']['label']}")
        print(f"  时间: {turn['timestamp']}")
        print(f"  回答长度: {len(turn['answer'])}")
    
    print("\n3. 测试会话摘要:")
    summary = agent.context_manager.get_session_summary(user_id)
    print(f"会话摘要: {summary}")
    
    print("\n=== 测试完成 ===")

if __name__ == "__main__":
    test_chat_history()


